package cn.zhibang.sannuo.account.api;

import cn.zhibang.sannuo.account.service.AAccountnumderService;
import cn.zhibang.sannuo.commons.model.Accountnumber;
import cn.zhibang.sannuo.commons.model.CommonResult;
import cn.zhibang.sannuo.commons.model.HeadOffice;
import cn.zhibang.sannuo.commons.model.Menu;
import cn.zhibang.sannuo.commons.utils.SaltUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author admin
 * @version 1.0.0
 * @ClassName AccountApi.java
 * @Description TODO
 * @createTime 2021年03月29日 16:22:00
 */
@RestController
@Api("账号管理")
public class AccountApi {

    @Autowired
    private CommonResult commonResult;
    @Autowired
    private AAccountnumderService aAccountnumderService;

    //登录
    @PostMapping("/sign_in")
    @ApiOperation("登录认证方法")
    public CommonResult sign_in(Accountnumber accountnumber){

        Subject subject = SecurityUtils.getSubject();

        UsernamePasswordToken token = new UsernamePasswordToken(accountnumber.getAccount(), accountnumber.getApssword());

        try {
            subject.login(token);
            commonResult.setCode(200);
            commonResult.setMaggers("登录成功");
            commonResult.setUrl("/index");
        }catch (UnknownAccountException e){
            commonResult.setCode(201);
            commonResult.setMaggers("用户名不存在");
        }catch (IncorrectCredentialsException e){
            commonResult.setCode(202);
            commonResult.setMaggers("密码不正确");
        }
        return commonResult;
    }

    //获得账号信息
    @PostMapping("/getAccountList")
    @ApiOperation("获得账号信息")
    public CommonResult getAccountList(String phone, String aname, String state,
                                       String province, String city, String county){
        if (phone == "")phone=null;
        if (aname == "")aname=null;
        if (state == "")state=null;
        if ("全部".equals(province))province=null;
        if ("全部".equals(city))city=null;
        if ("全部".equals(county))county=null;
        StringBuffer sAddreds = new StringBuffer();
        String addreds = null;
        String length = null;
        if (province != null){
            sAddreds.append(province);
            sAddreds.append(city);
            sAddreds.append(county);
            addreds = sAddreds.toString();
            length = addreds.length()+ "";
        }
        LinkedHashMap<String, Object> map = new LinkedHashMap<>();
        Subject subject = SecurityUtils.getSubject();
        Accountnumber accountnumber = (Accountnumber)subject.getPrincipal();
        map.put("account", accountnumber);
        String rnumber = aAccountnumderService.selAccountnumberRnumber(accountnumber.getAccount());
        if ("CJ".equals(rnumber)){
            List<Accountnumber> list = aAccountnumderService.selAccountnumberList(null, null, phone, aname, state, addreds, length);
            map.put("list", list);
        }
        if ("ZD".equals(rnumber)){
            String substring = accountnumber.getBranchOffice().getBnumber().substring(0,2);
            List<Accountnumber> list = aAccountnumderService.selAccountnumberList(substring, null, phone, aname, state, addreds, length);
            map.put("list", list);
        }
        if ("FD".equals(rnumber)){
            List<Accountnumber> list = aAccountnumderService.selAccountnumberList(null, accountnumber.getBranchOffice().getBnumber(), phone, aname, state, addreds, length);
            map.put("list", list);
        }
        commonResult.setData(map);
        return commonResult;
    }

    //获得总店信息
    @PostMapping("/getHead")
    @ApiOperation("获得总店信息")
    public CommonResult getHeadOffice(){
        List<HeadOffice> allHeadOffice = aAccountnumderService.getAllHeadOffice();
        LinkedHashMap<String, Object> map = new LinkedHashMap<>();
        map.put("allHeadOffice", allHeadOffice);
        commonResult.setData(map);
        return commonResult;
    }

    //设置账号
    @PostMapping("/setAccount")
    @ApiOperation("设置账号")
    public void setAccount(Accountnumber accountnumber, String bname){
        aAccountnumderService.upAccountnumber(accountnumber);
    }

    //重置密码
    @PostMapping("/setRepass")
    @ApiOperation("重置密码")
    public CommonResult setRepass(String account, String oldpass, String newpass){
        Accountnumber accountnumber = aAccountnumderService.selAccountnumber(account);
        String salt = SaltUtil.salt(oldpass, accountnumber.getSalt());
        if (!accountnumber.getApssword().equals(salt)){
            commonResult.setCode(202);
            commonResult.setMaggers("密码不正确");
        }else {
            String newSalt = SaltUtil.salt(newpass, accountnumber.getSalt());
            accountnumber.setApssword(newSalt);
            aAccountnumderService.upAccountnumber(accountnumber);
            commonResult.setCode(200);
        }
        return commonResult;
    }

    public CommonResult addAccountnumber(Accountnumber accountnumber, Integer bid, Integer rid){
        commonResult.setCode(200);
        commonResult.setMaggers("新增的账号信息为：");
//        commonResult.setData(accountnumber);
        return commonResult;
    }



}
